//通过Cookie获取所要修改模型的id
var getCookie = function (name) {
    var arr;
    var reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg)){
        return arr[2];
    }
    else
        return null;
};


var grid;
var form;
var form1;
$(function () {

    // 加载该页面前，先判断该模块的权限
    // 获取当前登录的用户的权限
    var permission = document.cookie.split(";")[1].split("=")[1];
    var b = String(permission).split('');
    for (var i = 0; i < b.length; i++) {
        console.log(b[i]);
    }
    var moudle22 = b[4];
    // 如果模块权限为1，即只能查询此模块下的数据，不能进行修改（禁用和重置密码不能使用）
    // 系统管理
    flag = true;
    if (moudle22 == 1) {
        $("#saveBtnMoudle").css('display', 'none');
    }

         //获取当前模型id
         var id = getCookie("id");

        var pageNo = 1;
        var pageSize = 5;
        var grid_selector = "#grid-table";
        var pager_selector = "#grid-pager";

        //初始化数据表格
        grid = $(grid_selector).jqGrid({
            datatype: "json",
            url: 'getQuestionList',
            mtype: 'POST',
            page: pageNo,
            postData: {
                'id': id,
            },
            height: 380,
            width: 300,
            colNames: [' ', '问题', '问题种类', '必填项', '问卷', '最高得分',],
            colModel: [
                //
                {
                    name: '',
                    index: '',
                    width: 30,
                    align: 'center',
                    sortable: false,
                    // formatter: function (cellvalue, options, rowObject) {
                    //     return "<button class='btn btn-group btn-xs' onclick='deleteProduct(\"" + rowObject.id + "\")'><i class='fa fa-edit button-in-i'></i>删除</button>";
                    // }
                },

                //问题
                {
                    name: 'content',
                    index: 'content',
                    width: 200,
                    align: 'center',
                    sortable: false,
                    // formatter: function (cellvalue, options, rowObject) {
                    //     return "<select onchange='productUpdateState(this.value," + rowObject.id + ")'><option value='0' style='display:none'>" + rowObject.productState + "</option><option value='1'>启用</option><option value='2'>暂停</option><option value='3'>终止</option></select>";
                    // }
                },

                //问题种类
                {name: 'questname', index: 'questname', sortable: false, align: 'center'},

                //必填项
                {
                    name: '',
                    index: '',
                    width: 120,
                    align: 'center',
                    sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        return "必填";
                    }
                },

                //是否参与问卷
                {
                    name: '',
                    index: '',
                    width: 120,
                    align: 'center',
                    sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        return "参与问卷";
                    }
                },

                //最高分数
                {
                    name: '',
                    index: '',
                    width: 120,
                    align: 'center',
                    sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        var data = [rowObject.A,rowObject.B,rowObject.C,rowObject.D];
                        var max = Math.max.apply(Math,data);
                        return max;
                    }
                },


            ],
            viewrecords: true,
            rowNum: pageSize,
            rownumbers: false,//添加左侧行号
            rowList: [5, 10, 15],
            pager: pager_selector,
            altRows: true,
            multiselect: false,
            loadComplete: function (data) {
                var table = this;
                setTimeout(function () {
                    updatePagerIcons(table);
                }, 0);
                var h = 187;
                $('.ui-jqgrid-bdiv').height(h);
            },
            autowidth: true,
            jsonReader: jsonReader,   // json中代表实际模型数据的入口
            rownumWidth: 55,
            prmNames: prmNames,
            recordtext: "显示 {0} - {1} 条 , 共 {2} 条",
            emptyrecords: "没有记录",
            loadtext: "加载中...",
            pgtext: "第 {0} 页  ,共 {1} 页"
        });

        //初始化分页条
        showNavGrid(grid_selector, pager_selector);

        //初始化信息校验表单
        form = $("#addform").validate({
            errorElement: 'div',
            errorClass: 'help-block messages',
            focusInvalid: false,
            highlight: validateHighlight,
            success: validateSuccess,
            errorPlacement: errorPlacement
        });
        form1 = $("#Editform").validate({
            errorElement: 'div',
            errorClass: 'help-block messages',
            focusInvalid: false,
            highlight: validateHighlight,
            success: validateSuccess,
            errorPlacement: errorPlacement
        });



    $("#modelName").val("");
    $("#productBank").val("");
    $("#division").val("");
    $("#productSerie").val("");
    $("#productName").val("");
    $("#customType").val("");
    $("#percentageDown").val("");
    $("#scoreDown").val("");
    $("#loanType").val("");
    $("#area").val("");
    //通过id获取所要修改模型的信息
    // console.log("当前模型id："+id);
    $.ajax({
        url: "getModelinfo?id="+id,
        type: "post",
        dataType: "json",
        success: function (data) {
            // console.log(data.modelList[0])
            $("#modelName").val(data.modelList[0].modelName);
            $("#productBank").val(data.modelList[0].bankname);
            $("#division").val(data.modelList[0].division);
            $("#productSerie").val(data.modelList[0].serieName);
            $("#productName").val(data.modelList[0].loanName);
            $("#customType").val(data.modelList[0].typeName);
            $("#percentageDown").val(data.modelList[0].percentageDown);
            $("#scoreDown").val(data.modelList[0].scoreDown);
            $("#loanType").val(data.modelList[0].loanType);
            $("#area").val(data.modelList[0].area);
        }
    })
})


//修改模型详细信息
function updateModelinfo() {
    if (confirm("确定要更改该产品的详细信息吗？")) {
        var id = getCookie("id");
        var modelName = $("#modelName").val();
        var division = $("#division").val();
        var percentageDown = $("#percentageDown").val();
        var scoreDown = $("#scoreDown").val();
        var loanType = $("#loanType").val();
        var area = $("#area").val();
        // console.log(modelName);
        // console.log(division);
        // console.log(percentageDown);
        // console.log(scoreDown);
        // console.log(loanType);
        // console.log(area);

        $.ajax({
            url: "updateModelinfo?modelName="+modelName+"&division="+division+"&percentageDown="+percentageDown+"&scoreDown="+scoreDown+"&loanType="+loanType+"&area="+area+"&id="+id,
            type: "post",
            success: function (data) {
                if (data.updateResult == 1) {
                    alert("信息修改成功");
                    grid.trigger('reloadGrid');
                    // loadPage('incremental/product_index.html');
                } else {
                    alert("信息修改失败");
                }
            },
            error: function (data) {
                alert("信息修改失败");
            }
        })
    }
}
